package com.dts.yjnb.service.service.impl;

import com.dts.yjnb.service.mapper.*;
import com.dts.yjnb.service.service.IWorkSpaceService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class WorkSpaceServiceImpl implements IWorkSpaceService {

    private static final Logger log = LoggerFactory.getLogger(WorkSpaceServiceImpl.class);
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private AgriculturalMapper agriculturalMapper;
    @Autowired
    private CreditInfoMapper creditInfoMapper;
    @Autowired
    private MarketMapper marketMapper;

    @Override
    public Map<String, List<Integer>> getRecent12MonthsLineChartData() {
        // 计算最近12个月（不包括本月）
        Calendar calendar = Calendar.getInstance();

        // 获取当前年月（用于日志）
        int currentYear = calendar.get(Calendar.YEAR);
        int currentMonth = calendar.get(Calendar.MONTH) + 1;
        log.info("当前年月:{}、{}", currentYear, currentMonth);

        // 计算上个月（排除本月）
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        calendar.add(Calendar.MONTH, -1); // 往前推1个月，排除本月

        int lastMonthYear = calendar.get(Calendar.YEAR);
        int lastMonth = calendar.get(Calendar.MONTH) + 1;

        // 计算开始月份（往前推11个月）
        Calendar startCalendar = (Calendar) calendar.clone();
        startCalendar.add(Calendar.MONTH, -11);
        int startYear = startCalendar.get(Calendar.YEAR);
        int startMonth = startCalendar.get(Calendar.MONTH) + 1;

        // 打印查询时间范围日志
        log.info("当前时间: {}年{}月", currentYear, currentMonth);
        log.info("查询时间范围: {}年{}月 至 {}年{}月", startYear, startMonth, lastMonthYear, lastMonth);

        //查询最近12月份的数据
        List<Integer> productCenterData = productMapper.get12MonthData(startYear, startMonth, lastMonthYear, lastMonth);
        List<Integer> agriculturalServiceData = agriculturalMapper.get12MonthData(startYear, startMonth, lastMonthYear, lastMonth);
        List<Integer> creditInfoData = creditInfoMapper.get12MonthData(startYear, startMonth, lastMonthYear, lastMonth);

        log.info("creditInfoData: {}", creditInfoData);

        // 封装返回结果
        Map<String, List<Integer>> result = new HashMap<>();
        result.put("productCenter", productCenterData);
        result.put("agriculturalService", agriculturalServiceData);
        result.put("creditInfo", creditInfoData);

        return result;
    }

    @Override
    public Map<String, Object> getBarChartData() {
        // 在一个查询中同时获取省份名称和数量，确保顺序一致
        List<Map<String, Object>> provinceData = marketMapper.countMarketByProvince();

        log.info("柱状图数据:{}", provinceData);

        //封装成前端需要的格式
        List<String> categories = new ArrayList<>();
        List<Integer> values = new ArrayList<>();
        for (Map<String, Object> item : provinceData) {
            categories.add((String) item.get("provinceName"));
            values.add(((Number) item.get("count")).intValue());
        }

        Map<String, Object> result = new HashMap<>();
        result.put("categories", categories);
        result.put("values", values);

        return result;
    }

    @Override
    public List<Map<String, Object>> getPieChartData() {
        // 从数据库查询各产品分类的统计数据
        List<Map<String, Object>> categoryData = productMapper.countProductByCategory();

        log.info("饼图数据:{}", categoryData);

        // 将数据库返回的数据转换为前端需要的格式（name和value）
        List<Map<String, Object>> result = new ArrayList<>();
        for (Map<String, Object> item : categoryData) {
            Map<String, Object> pieItem = new HashMap<>();
            pieItem.put("name", item.get("name"));
            pieItem.put("value", ((Number) item.get("value")).intValue());
            result.add(pieItem);
        }

        return result;
    }
}
